package com.sicnu.cheer.im.activity;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sicnu.cheer.generalmodule.activity.BaseActivity;
import com.sicnu.cheer.generalmodule.bean.LeagueBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.bean.NoticeBean;
import com.sicnu.cheer.generalmodule.util.StringUtils;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.generalmodule.widget.NoDoubleClickListener;
import com.sicnu.cheer.im.R;

import java.io.File;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by cheer on 2017/2/13.
 * 发送通知界面
 */
public class LaunchNoticeActivity extends BaseActivity implements View.OnClickListener {
    private EditText titleET;
    private EditText contentET;
    private TextView countTV;
    private ProgressDialog progressDialog;
    private LeagueBean leagueBean;

    @Override
    public int getContentViewId() {
        return R.layout.activity_launch_notice;
    }

    protected void initView() {
        super.initView();
        ((TextView) findViewById(R.id.title)).setText("新建通知");
        findViewById(R.id.back).setOnClickListener(this);
        titleET = ((EditText) findViewById(R.id.notice_title_et));
        contentET = ((EditText) findViewById(R.id.notice_content_et));
        countTV = ((TextView) findViewById(R.id.character_count_tv));
        contentET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                countTV.setText(contentET.getText().toString().length() + "/300");
            }
        });

        findViewById(R.id.launch_notice_submit_btn).setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onClick(View v) {
                super.onClick(v);
                if (leagueBean == null) {
                    UIHelper.ToastMessage(mThis, "请设置您当前所在社团");
                } else {
                    CheckAndSubmit();
                }
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        progressDialog = new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        MyUser currentUser = MyUser.getCurrentUser(mThis, MyUser.class);
        if (currentUser.getLeague() != null) {
            BmobQuery<LeagueBean> query = new BmobQuery<>();
            query.addWhereEqualTo("objectId", currentUser.getLeague().getObjectId());
            query.findObjects(mThis, new FindListener<LeagueBean>() {
                @Override
                public void onSuccess(List<LeagueBean> list) {
                    if (list != null && list.size() >= 0) {
                        leagueBean = list.get(0);
                    }
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        }

    }

    /**
     * 检测输入合法性并提交
     */
    private void CheckAndSubmit() {
        final String title = titleET.getText().toString().trim();
        final String content = contentET.getText().toString().trim();
        if (StringUtils.isEmpty(title)) {
            UIHelper.ToastMessage(mThis, "请填写通知标题");
            return;
        }
        if (StringUtils.isEmpty(content)) {
            UIHelper.ToastMessage(mThis, "请输入通知内容");
            return;
        }
        progressDialog.show();
        NoticeBean noticeBean = new NoticeBean();
        MyUser currentUser = MyUser.getCurrentUser(mThis, MyUser.class);
        noticeBean.setAuthor(currentUser);
        noticeBean.setLeague(leagueBean);
        noticeBean.setTitle(title);
        noticeBean.setContent(content);
        noticeBean.save(mThis, new SaveListener() {
            @Override
            public void onSuccess() {
                dismissProgress();
                UIHelper.ToastMessage(mThis, "通知创建成功");
                finish();
            }

            @Override
            public void onFailure(int i, String s) {
                dismissProgress();
                UIHelper.ToastMessage(mThis, "通知创建失败：" + s);
            }
        });
    }

    private void dismissProgress() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            onBackPressed();
        }
    }
}
